package com.module.base.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import com.module.base.R;
import com.module.frame.base.BaseFrameApplication;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author bsnl_yanxuwen
 * @date 2021/1/21 11:06
 * Description :
 */
public class CommonUtils {
    /**
     * 获取string字符串
     *
     * @param resId
     * @return
     */
    public static String getString(int resId) {
        return getResource().getString(resId);
    }

    /**
     * 获取resources
     *
     * @return Resources
     */
    public static Resources getResource() {
        return BaseFrameApplication.getContext().getResources();
    }

    public static void copyToClipBoard(Context context, String text) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText(BaseFrameApplication.getContext().getResources().getString(R.string.app_name), text);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
    }


    /**
     * 获取剪切板上的内容
     */
    public static String getClipboardContent(Context context) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (cm != null) {
            ClipData data = cm.getPrimaryClip();
            if (data != null && data.getItemCount() > 0) {
                ClipData.Item item = data.getItemAt(0);
                if (item != null) {
                    CharSequence sequence = item.coerceToText(context);
                    if (sequence != null) {
                        return sequence.toString();
                    }
                }
            }
        }
        return "";
    }

    public static boolean  isNetwork(){
        ConnectivityManager connectivityManager = (ConnectivityManager) BaseFrameApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo!=null){
            return activeNetworkInfo.isAvailable();
        }
        return false ;
    }

    /**
     * 提取url
     */
    public static String pickUpUrl(String url) {
        ArrayList<String> links = new ArrayList();

        String regex = "(http://|https://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(url);
        while (m.find()) {
            String urlStr = m.group();
            if (urlStr.startsWith("(") && urlStr.endsWith(")")) {
                urlStr = urlStr.substring(1, urlStr.length() - 1);
            }
            links.add(urlStr);
        }
        if (!CollectionUtil.isEmptyOrNull(links)) {
            return links.get(0);
        } else {
            return "";
        }
    }


    /**
     * 从剪切板获得文字。
     */
    public static String getClipboardText() {
        ClipboardManager manager = (ClipboardManager) BaseFrameApplication.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        if (manager != null) {
            if (manager.hasPrimaryClip() && manager.getPrimaryClip().getItemCount() > 0) {
                CharSequence addedText = manager.getPrimaryClip().getItemAt(0).getText();
                String addedTextString = String.valueOf(addedText);
                if (!TextUtils.isEmpty(addedTextString)) {
                    return addedTextString;
                }
            }
        }
        return "";
    }

    /**
     * 从剪切板获得文字。
     */
    public static String getInviteCode() {
        try {
            String text = getClipboardText();
            if (text.contains("【") || text.contains("】")) {
                text = text.substring(text.indexOf("【") + 1, text.indexOf("】"));
                return text;
            }
            return "";
        } catch (Exception e) {
            return "";
        }
    }
}
